package cn.kgc.dao;

import cn.kgc.util.ConfigManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;

/**
 * 数据库操作的基类
 */
public class BaseDao {
    protected Connection con = null;
    protected PreparedStatement ps = null;
    protected Statement st = null;
    protected ResultSet rs = null;

    //获取数据库连接
    public boolean getConnection(){
        try {
            Class.forName(ConfigManager.getInstance().getString("jdbc.driver"));
            String url = ConfigManager.getInstance().getString("jdbc.connection.url");
            String username = ConfigManager.getInstance().getString("jdbc.connection.username");
            String password = ConfigManager.getInstance().getString("jdbc.connection.password");
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //获取数据库连接：通过数据源获取数据库连接
    public boolean getConnection2(){
        try {
            //初始化上下文
            Context cxt = new InitialContext();
            //获取与逻辑名称相关联的数据源对象
            DataSource ds = (DataSource) cxt.lookup("java:comp/env/jdbc/news");
            //通过数据源获取数据库连接
            con = ds.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //增删改
    //String sql="update news_detail set name=? where id=?";
    //params = {"张三",3}
    //ps,setString(1,params[0]);
    //ps.setInt(2,params[1]);
    public int executeUpdate(String sql,Object[] params){
        int updateRows = 0;
        if(getConnection()){
            try {
                ps = con.prepareStatement(sql);
                //填充占位符
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
                updateRows = ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return updateRows;
    }


    //查询
    public ResultSet executeQuery(String sql,Object[] params){
        if(getConnection()){
            try {
                ps = con.prepareStatement(sql);
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
                rs = ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rs;
    }

    //查询：通过数据源获取连接
    public ResultSet executeQuery2(String sql,Object[] params){
        if(getConnection2()){
            try {
                ps = con.prepareStatement(sql);
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
                rs = ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rs;
    }



    //关闭资源
    public void closeResource(){
        if (rs != null)
            try {
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        if (ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


}
